﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XYZCompiler.Properties;

namespace XYZCompiler.ResultDialog
{
    public partial class SemanticResult : Form
    {
        public SemanticResult(object result)
        {
            InitializeComponent();
            table = result as Dictionary<string, XYZType>;
            if(table == null)
            {
                MessageBox.Show(Resources.SemanticResult_SemanticResult_Error_param);
                this.DialogResult = DialogResult.Abort;
            }
        }
        public void VisteTable()
        {
            var root = typeTree.Nodes.Add("Root");
            foreach (var xyzType in table)
            {
                var tNode = root.Nodes.Add(xyzType.Key);
                var varNode = tNode.Nodes.Add("Class Varibal");
                tNode.Tag = xyzType.Value.row;
                foreach (var type in xyzType.Value._varTable)
                {
                    varNode.Nodes.Add(type.Key+':'+type.Value.Name);
                }
                var methods = tNode.Nodes.Add("Methods");
                foreach (var xyzMethod in xyzType.Value._methodTable)
                {
                    var methodNode = methods.Nodes.Add(xyzMethod.Name);
                    methodNode.Tag = xyzMethod.row;
                    var paramsNode = methodNode.Nodes.Add("Params");
                    foreach (var type in xyzMethod._params)
                    {
                        paramsNode.Nodes.Add(type.Key+':'+type.Value.Name).Tag = methodNode.Tag;
                    }
                    var loacalNode = methodNode.Nodes.Add("Local variables");
                    foreach (var variableInfo in xyzMethod._variableInfo)
                    {
                        loacalNode.Nodes.Add(variableInfo.Key+':'+variableInfo.Value.type.Name).Tag = variableInfo.Value.r;
                    }
                }
            }
        }
        private Dictionary<string,XYZType> table = null;

        private void SemanticResult_Load(object sender, EventArgs e)
        {
            VisteTable();
        }


        public delegate void MyInvoke(int r);
        private void typeTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            if (node == null || node.Tag == null)
                return;
            int row;
            if (!int.TryParse(node.Tag.ToString(), out row))
                return;
            Program.main.BeginInvoke((MyInvoke)SelectRow,new object[]{row});

        }
        private void SelectRow(int r)
        {
            CompilerHelper.SelectWord(r,0);
            //Program.main.Activate();
            //Program.main.Focus();
            //MessageBox.Show("1");
        }
    }
}
